package com.baizan.daka.ui.presenter

import android.util.Log
import android.widget.Toast
import com.baizan.daka.base.BasePresenter
import com.baizan.daka.mvp.model.TaskModel
import com.baizan.daka.mvp.model.UploadModel
import com.baizan.daka.rx.Subscriber.CommonSubscriber
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.ReceiveTaskContract
import com.hazz.kotlinmvp.net.BaseResponse
import io.reactivex.SingleObserver
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import rx.Subscriber
import java.io.File
import java.util.ArrayList

class ReceiveTaskPresenter(val mView: ReceiveTaskContract.View) : BasePresenter(),
    ReceiveTaskContract.Presenter {

    private val uploadModel: UploadModel by lazy {
        UploadModel()
    }
    private val taskModel: TaskModel by lazy {
        TaskModel()
    }


    private fun submitTask(id: String, remarks: String, fileUrl: List<String>) {
        taskModel.submitTask(id, remarks, fileUrl)
            .subscribe(object : CommonSubscriber<String>(compositeDisposable) {

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallNext(t: String?) {
                    mView.submitSuccess(t!!)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }

    override fun submit(id: String, remarks: String, files: List<String>) {
        mView.showLoadingDialog()
        uploadModel.upload(files)
            .toList()
            .subscribe(object : SingleObserver<List<BaseResponse<String>>> {
                override fun onSuccess(t: List<BaseResponse<String>>) {
                    val urls: MutableList<String> = ArrayList()
                    for (value in t){
                        urls.add(value.data!!)
                    }
                    submitTask(id, remarks, urls)
                }

                override fun onSubscribe(d: Disposable) {

                }

                override fun onError(e: Throwable) {
                    mView.hideLoadingDialog()
                }

            })
    }
}

